#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <pthread.h>

#include "menu.h"
#include "song.h"
#include "list.h"

int fd[2];
int is_run = 1;
char song_L[100],song_P[100];
float song_l,song_p;

void *lyrics_display(void *arg)
{
	pthread_detach(pthread_self());

	char a[50] = {0};
	char buf[150] = {0};
	Sing *p;
	int minute;
	float sec;

	p = (Sing *)malloc(sizeof(Sing));
	p = (Sing *)arg;
	strcpy(a,p->sing_path);
	strcpy(a+strlen(a)-3,"txt");
	
//	printf("BBBBBBB%s\n",p->sing_path);
//	printf("CCCCCCC%s\n",a);

	FILE *fp;
	fp = fopen(a,"r");
	while(!feof(fp))
	{
		memset(buf,0,sizeof(buf));
		fgets(buf,100,fp);
		sscanf(buf+1,"%d",&minute);
		sscanf(buf+4,"%f",&sec);
	//	printf("%02d,%5.2f\n",minute,sec);
		while(1)
		{	
			if(!is_run)
				pthread_exit(NULL);
			if((minute * 60 + sec) < song_p + 0.1)
				break;
			sleep(0.5);
		}
		printf("%s\n",buf);
		
	}

	return NULL;
}

void *send_timeorder(void *arg)
{
	pthread_detach(pthread_self());

	int i = 1;
	
	while(i++)
	{
		if(!is_run)
				pthread_exit(NULL);
		send_message("get_time_pos\n");
		sleep(1);
		if(i == 12)
		{
			send_message("get_time_length\n");
			sleep(1);
		}		
	}
	return NULL;
}

void *read_timeorder(void *arg)
{
	pthread_detach(pthread_self());

	char buf[100] = {0};

	char spam[100];
	while(read(fd[0],spam,sizeof(spam)))
	{
		if(!strncmp(spam,"ANS_TIME_POSITION",17))
			break;
		memset(spam,0,sizeof(spam));
	}	
	sscanf(spam+18,"%f",&song_p);

	while(read(fd[0],buf,sizeof(buf)))
	{
		if(!is_run)
				pthread_exit(NULL);
	//	read(fd[0],buf,sizeof(buf));
		if(!strncmp(buf,"ANS_TIME_POSITION",17))
		{
			memset(song_P,0,sizeof(song_P));
			strcpy(song_P,buf);
			sscanf(buf+18,"%f",&song_p);
		//	printf("P:%sp:%5.2f\n",song_P,song_p);
		}
		if(!strncmp(buf,"ANS_LENGTH",10))
		{
			memset(song_L,0,sizeof(song_L));
			strcpy(song_L,buf);
			sscanf(buf+11,"%f",&song_l);
		//	printf("L:%sl:%6.2f\n",song_L,song_l);
		}
		
	//	printf("read_timeorder:%s\n",buf);
		sleep(1);
		memset(buf,0,sizeof(buf));
	}
	return NULL;
}


Sing* play_song()
{
	Sing *p;
//	int fd_f;
//	int fd[2];
	pipe(fd);
	p = read_singlist();
	if(access("lc_fifo",F_OK) != 0)
		mkfifo("lc_fifo",0666);

	pid_t pid = fork();
	if (pid == 0)
	{
		close(fd[0]);
		dup2(fd[1],1);
		close(fd[1]);
		execl("/usr/bin/mplayer","mplayer","-idle","-slave","-quiet","-input","file=./lc_fifo",p->sing_path,NULL);
	} 
	else
	{
		close(fd[1]);
	//	printf("AAAAAAAA%s\n",p->sing_path);
		pthread_t tid1,tid2,tid3;
		pthread_create(&tid2,NULL,send_timeorder,NULL);
		pthread_create(&tid3,NULL,read_timeorder,NULL);
		pthread_create(&tid1,NULL,lyrics_display,p);
	}
	return p;

}

void send_message(char *buf)
{
	int fd = open("lc_fifo",O_WRONLY);
	write(fd,buf,strlen(buf));
//	system("echo \"pause\" > lc_fifo");
	close(fd);	
}


Sing* next_sing(Sing *singing)
{
	char buf[60];
	Sing *head,*p;
	head = read_singlist();
	p = head;
//	printf("head:%s\n",head->sing_path);
//	printf("sing:%s\n",singing->sing_path);
	while(p->next->next != NULL)
		p = p->next;
//	printf("p:%s\n",p->sing_path);
	if(!strcmp(p->sing_path,singing->sing_path))
		p = head;
	else
	{
		p = head;
		while(p->next != NULL)
		{
			if(!strcmp(p->sing_path,singing->sing_path))
				break;
			p = p->next;
		}
		p = p->next;
	}
//	printf("p:%s\n",p->sing_path);
	strcpy(buf,"loadfile ");
	strcat(buf,p->sing_path);
	strcat(buf,"\n");
//	printf("%s\n",buf);
	send_message(buf);
	return p;
}

Sing* last_song(Sing *singing)
{
	char buf[60];
	Sing *head,*p,*tp;
	head = read_singlist();
	p = head;
	if(!strcmp(head->sing_path,singing->sing_path))
	{
		while(p->next->next != NULL)
			p = p->next;
	}
	else
	{
		tp = p; 
		p = p->next;
		while(p->next != NULL)
		{
		if(!strcmp(p->sing_path,singing->sing_path))
				break;
			tp = p;
			p = p->next;
		}
		p = tp;
	}

	strcpy(buf,"loadfile ");
	strcat(buf,p->sing_path);
	strcat(buf,"\n");
	printf("%s\n",buf);
	send_message(buf);
	return p;
}

Sing* play_a_song()
{
	int i,num;

	printf("input the num of song :\n");
	scanf("%d",&num);
	getchar();

	char buf[60];
	Sing *head,*p;
	head = read_singlist();

	p = head;
	for (i = 1;i < num ;i++)
		p = p->next;
	strcpy(buf,"loadfile ");
	strcat(buf,p->sing_path);
	strcat(buf,"\n");
	printf("%s\n",buf);
	send_message(buf);
	return p;
}

void fast_forward()
{
	int num;

	printf("input the second to ff :\n");
	scanf("%d",&num);
	getchar();

	char buf[30],s[5];
	strcpy(buf,"seek ");
	sprintf(s,"%d",num);
	strcat(buf,"+");
	strcat(buf,s);
	strcat(buf,"\n");

	send_message(buf);

}

void back_forward()
{
	int num;

	printf("input the second to bf :\n");
	scanf("%d",&num);
	getchar();

	char buf[30],s[5];
	strcpy(buf,"seek ");
	sprintf(s,"%d",num);
	strcat(buf,"-");
	strcat(buf,s);
	strcat(buf,"\n");

	send_message(buf);

}

void set_volume()
{
	int num;

	printf("请输入要设置的音量:\n");
	scanf("%d",&num);
	getchar();

	char buf[30],s[5];
	strcpy(buf,"volume ");
	sprintf(s,"%d",num);
	strcat(buf,s);
	strcat(buf," 1");
	strcat(buf,"\n");

	send_message(buf);
}


